﻿//创建房间
package com.victim.snake.graphics
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextFieldType;
	import flash.events.Event;




	import com.victim.snake.user.User;
	import com.victim.snake.user.ProcessMsg;
	import fl.controls.Label;
	import flash.display.Graphics;
	import fl.controls.Button;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	import flash.net.drm.VoucherAccessInfo;
	import fl.controls.List;
	import com.victim.snake.graphics.createRoomClass.controls.ComboBox;
	import flash.display.Bitmap;
	import com.victim.snake.util.Loader;
	import flash.display.LoaderInfo;
	import com.victim.snake.map.Map;
	import com.victim.snake.map.MapEvent;
	import flash.utils.Dictionary;
	import com.victim.snake.util.Image;
	
	//游戏主界面
	public class CreateRoomSprite extends Sprite
	{

		
		var okBut:Button = null;
		var cancelBtn:Button = null;
		var roomName:TextField = null;
		var passwordText:TextField=null;
		var waitLab:Label=new Label();
		var mapNameList:ComboBox=new ComboBox();
		var map:Map=null;
		var previewMap:Bitmap=null;
		public var modeList:ComboBox=new ComboBox();
		private var backGround:Image=null;
		

		public function CreateRoomSprite(mapName:String="default"):void
		{
			//下载创建房间对话框架背景
			new Loader("image/main/createRoom/backGround.png",backLoaderEvent);
			//加载创建房间对话框确认按钮未选择切换且鼠标光标不在按钮上方时的图标
			new Loader("image/main/createRoom/okBtnUp.png",okBtnUpLoaderEvent);
			//加载创建房间对话框确认按钮未选择切换且鼠标光标在按钮上方时的图标
			new Loader("image/main/createRoom/okBtnOver.png",okBtnOverLoaderEvent);
			//加载创建房间对话框确认按钮未选择切换且鼠标按下按钮时的图标
			new Loader("image/main/createRoom/okBtnDown.png",okBtnDownLoaderEvent);
			//加载创建房间对话框关闭按钮未选择切换且鼠标光标不在按钮上方时的图标
			new Loader("image/main/createRoom/cancelBtnUp.png",cancelBtnUpLoaderEvent);
			//加载创建房间对话框关闭按钮未选择切换且鼠标光标在按钮上方及按下时的图标
			new Loader("image/main/createRoom/cancelBtnOver.png",cancelBtnOverLoaderEvent);
			
			
			//设置房间要显示的地图
			map=Map.getMap(mapName);

			map.addEventListener(MapEvent.IsLoader,previewImageLoaderEvent);
			map.load();

			/*var graphics:Graphics = this.graphics;
			graphics.beginFill(0xcccccc,1);
			graphics.drawRect(100,70,300,200);
			graphics.endFill();
			graphics.beginFill(0xffffff,1);
			graphics.drawRect(101,71,298,198);
			graphics.endFill();*/

			

			roomName=new TextField();
			roomName.x = 320;
			roomName.y = 160;
			roomName.width = 101;
			roomName.height = 18;
			roomName.border = false;
			roomName.type = TextFieldType.INPUT;
			roomName.text=User.getPlayer().getUserName()+"的房间";
			this.addChild(roomName);

			passwordText=new TextField();
			passwordText.width = 101;
			passwordText.height = 18;
			passwordText.x = 320;
			passwordText.y = 190;
			passwordText.border = false;
			passwordText.type = TextFieldType.INPUT;
			passwordText.displayAsPassword = true;
			this.addChild(passwordText);

			okBut=new Button();
			okBut.label="";
			okBut.width=179;
			okBut.height=32;
			okBut.x = 309;
			okBut.y = 255;
			okBut.addEventListener(MouseEvent.CLICK,newRoom);
			this.addChild(okBut);
			
			cancelBtn=new Button();
			cancelBtn.x = 529;
			cancelBtn.y = 132;
			cancelBtn.width=21;
			cancelBtn.height=20;
			cancelBtn.label = "";
			cancelBtn.addEventListener(MouseEvent.CLICK,cancel);
			this.addChild(cancelBtn);
			
			//地图名下拉菜单
			mapNameList.width=103;
			mapNameList.height=16;
			mapNameList.x=437;
			mapNameList.y=158;
			//mapNameList.setStyle("upSkin",CreateRoomComboBoxUpSkin);
			mapNameList.addEventListener(Event.CHANGE,mapNameListChangeEvent);
			this.addChild(mapNameList);
			
			//游戏模式选择下拉框
			this.modeList.addItemAt({label:"吃货",data:0},0);
			this.modeList.addItemAt({label:"死亡",data:1},1);
			modeList.width=103;
			modeList.height=16;
			modeList.x=320;
			modeList.y=222;			
			//modeList.width=90;
			this.addChild(modeList);
			
			waitLab.text = "wait...";
			waitLab.x = 110;
			waitLab.y = 78;
			waitLab.visible=false;
			this.addChild(waitLab);
			User.getStage().focus=roomName;	//设置焦点

		}
		
		//背景画面加载
		public function backLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			var bg:Bitmap=Bitmap(loaderInfo.content);
			bg.x=241;
			bg.y=101;
			this.addChildAt(Bitmap(loaderInfo.content),0);
		}
		
		//设置创建房间对话框确认按钮未选择切换且鼠标光标不在按钮上方时的图标
		public function okBtnUpLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.okBut.setStyle("upSkin",Bitmap(loaderInfo.content));
		}
		//设置创建房间对话框确认按钮未选择切换且鼠标光标在按钮上方时的图标
		public function okBtnOverLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.okBut.setStyle("overSkin",Bitmap(loaderInfo.content));
		}
		//设置创建房间对话框确认按钮未选择切换且按下按钮时的图标
		public function okBtnDownLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.okBut.setStyle("downSkin",Bitmap(loaderInfo.content));
		}
		
		//设置创建房间对话框关闭按钮未选择切换且鼠标光标不在按钮上方时的图标
		public function cancelBtnUpLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.cancelBtn.setStyle("upSkin",Bitmap(loaderInfo.content));			
		}
		//设置创建房间对话框关闭按钮未选择切换且鼠标光标在按钮上方时的图标
		public function cancelBtnOverLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.cancelBtn.setStyle("overSkin",Bitmap(loaderInfo.content));
			this.cancelBtn.setStyle("downSkin",Bitmap(loaderInfo.content));
		}
		
		
		
		//服务器发来消息27后调用
		public function addMapItem(map:Map):void{
			mapNameList.addItem({label:map.getCName(),data:map.getName()});
		}


		//下载地图预览
		function previewImageLoaderEvent(event:MapEvent):void{
			this.previewMap=new Bitmap(event.map.getPreviewImage().bitmapData);
			this.previewMap.width=103;
			this.previewMap.height=62;
			this.previewMap.x=437;
			this.previewMap.y=179;
			//加载地图预览图到画面			
			this.addChild(this.previewMap);
		}

		//地图选择
		public function mapNameListChangeEvent(e:Event):void{
			//trace(mapNameList.selectedItem.data);
			if(this.previewMap!=null){
				this.removeChild(previewMap);
			}
			this.map=Map.getMap(mapNameList.selectedItem.data);
			map.addEventListener(MapEvent.IsLoader,previewImageLoaderEvent);
			map.load();
			
		}

		//点击创始房间按钮,提交创建房间申请
		public function newRoom(event:MouseEvent):void
		{
			ProcessMsg.socketSend(ProcessMsg.generateUTF8Msg(5,roomName.text+";"+map.getName()+";"+this.modeList.selectedItem["data"]));
			this.showWait();
			//var roomSprite:RoomSprite=new RoomSprite();
			//User.getStage().removeChildAt(0);//删除主界面
			//User.getStage().addChildAt(roomSprite,0);//添加房间界面

		}
		
		//点击取消创建房间按钮，取消房间创建
		public function cancel(event:MouseEvent):void
		{
			User.getMainSprite().removeChild(this);
		}

		//删除子显示对象
		public function removeSub():void
		{			
				this.roomName.visible=false;
				//this.roomNameLable.visible=false;
				this.okBut.visible=false;
				this.cancelBtn.visible=false;
				//this.removeChildAt(2);
				//this.removeChildAt(1);
				//this.removeChildAt(0);			
		}
		
		//初使化创建房间对话框
		public function init():void{
			this.waitLab.visible=false;
			this.roomName.visible=true;
			this.okBut.visible=true;
			this.cancelBtn.visible=true;
		}

		public function showWait():void
		{
			this.removeSub();
			waitLab.visible=true;			
		}

	}

}